package com.xnyzc.lhy.order.controller.capi.order;

import com.xnyzc.lhy.common.component.didi.DiDiUrlConstant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.DiDiException;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.common.util.sign.AttestationTool;
import com.xnyzc.lhy.order.entity.mq.OrderParam;
import com.xnyzc.lhy.order.entity.param.order.ForecastPriceParam;
import com.xnyzc.lhy.order.entity.param.order.OrderExpressParam;
import com.xnyzc.lhy.order.service.order.IOrderExpressService;
import com.xnyzc.lhy.order.service.order.IPriceCorrelationService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  订单相关
 * </p>
 *
 */
@RestController
@RequestMapping(value = "/oa/auth")
public class OaOrderController {

    @Autowired
    private IOrderExpressService orderExpressService;

    @Autowired
    private IPriceCorrelationService priceCorrelationService;

    /**
     * 预估价格
     *
     * @param forecastPriceParam
     * @return
     */
    @ApiOperation(value = "预估价格", notes = "预估价格")
    @PostMapping(value = "/calcExpress")
    @ResponseBody
    public Rv getForecastPriceReal(@RequestBody ForecastPriceParam forecastPriceParam) {

        if (!AttestationTool.getRequestSign(forecastPriceParam, forecastPriceParam.getSign(), DiDiUrlConstant.didiSignReq)) {
            throw DiDiException.create(EDiDiErrorCode.failureOfVerification);
        }
        return priceCorrelationService.getForecastPriceReal(forecastPriceParam);
    }

    /**
     * 查询订单详情
     * @return
     */
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    @GetMapping(value = "/getOrderDetails")
    public Rv getOrderDetails(@ApiParam(name = "orderId", value = "订单号") @RequestParam(value = "orderId") String orderId){

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return orderExpressService.getOrderDetail(orderId,userId);
    }

    /**
     * 订单状态查询
     * @return
     */
    @ApiOperation(value = "订单状态查询", notes = "订单状态查询")
    @GetMapping(value = "/getOrderStatus")
    public Rv getOrderStatus(@ApiParam(name = "orderId", value = "订单号") @RequestParam(value = "orderId") String orderId){

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return orderExpressService.getOrderStatus(orderId,userId);
    }

    /**
     * 创建订单
     *
     * @param
     * @return
     */
    @ApiOperation(value = "创建订单", notes = "创建订单")
    @PostMapping(value = "/callExpress")
    @ResponseBody
    public Rv getCreateOrder(@RequestBody OrderParam orderParam) {

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        if (StringUtils.isEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        orderParam.setUserId(userId);
        orderParam.setTag(tag);
        return orderExpressService.getCreateOrder(orderParam);
    }

    /**
     * 取消订单 （仅支持乘客端）
     *
     * @param orderParam
     * @return
     */
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @PostMapping(value = "/cancel")
    @ResponseBody
    public Rv cancelOrder(@RequestBody OrderParam orderParam) {

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        orderParam.setUserId(userId);
        return orderExpressService.cancelOrder(orderParam);
    }

    /**
     * 账单详情
     *
     * @param orderExpressParam
     * @return
     */
    @ApiOperation(value = "账单详情", notes = "账单详情")
    @PostMapping(value = "/billDetails")
    public Rv billDetails(@ApiParam(name =
            "orderDetailParam", value = "查询订单详情") OrderExpressParam orderExpressParam) {
        if (!AttestationTool.getRequestSign(orderExpressParam, orderExpressParam.getSign(), DiDiUrlConstant.didiSignReq)) {
            throw DiDiException.create(EDiDiErrorCode.failureOfVerification);
        }
        return orderExpressService.billDetails(orderExpressParam);
    }

    /**
     * 我的行程 乘客端
     *
     * @param orderParam
     * @return
     */
    @ApiOperation(value = "我的行程", notes = "我的行程")
    @PostMapping(value = "/getUserOrder")
    @ResponseBody
    public Rv getUserOrder(@RequestBody OrderParam orderParam) {

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        orderParam.setUserId(userId);
        if(orderParam.getPageNum()==null|| orderParam.getPageNum()<1){
            orderParam.setPageNum(1);
        }
        if(orderParam.getPageSize()==null||orderParam.getPageSize()<1||orderParam.getPageSize()>100){
            orderParam.setPageSize(10);
        }
        return orderExpressService.getUserOrder(orderParam);
    }

    /**
     * 删除订单 （仅支持乘客端）  只能删除已取消和已完成订单
     *
     * @param orderParam
     * @return
     */
    @ApiOperation(value = "删除订单", notes = "删除订单")
    @PostMapping(value = "/deleteOrder")
    @ResponseBody
    public Rv deleteOrder(@RequestBody OrderParam orderParam) {

        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        orderParam.setUserId(userId);
        return orderExpressService.deleteOrder(orderParam);
    }


}
